from django.shortcuts import render_to_response
from .models import *
from django.http import HttpResponseRedirect
from django.http import JsonResponse
from django.core.urlresolvers import reverse
# Create your views here.

def valid_login(func):
    def inner(request, *args, **kwargs):
        username = request.session.get('username', '')
        if username:
            userData = Users.objects.get(user = username)
            all_dict = {
                'id' : userData.id,
                'user' : userData.user,
                'password' : userData.password,
                'name' : userData.name,
                'city' : userData.city,
                'email' : userData.email,
                'phone' : userData.phone,
            }
            request.session['userData'] = all_dict
            return func(request)
        else:
            return HttpResponseRedirect('/user/login')
    return inner


def userValid(user):
    if Users.objects.filter(user = user):
        return True
    else:
        return False


def login(request):
    if request.method == 'POST' and request.POST:
        user = request.POST['user']
        password = request.POST['password']
        if userValid(user):
            passwd = Users.objects.filter(user = user)[0].password
            if password == passwd:
                response = HttpResponseRedirect('/user/index')
                response.set_cookie('user',user)
                request.session['username'] = user
                return response
            else:
                return HttpResponseRedirect('/user/login')
        else:
            return HttpResponseRedirect('/uses/login')
    else:
        return render_to_response('signin.html', locals())


def register(request):
    if request.method == 'POST' and request.POST:
        u = Users()
        u.user = request.POST['user']
        u.password = request.POST['password']
        u.name = request.POST['name']
        u.city = request.POST['city']
        u.email = request.POST['email']
        u.phone = request.POST['phone']
        u.save()
        return HttpResponseRedirect('/user/login')
    else:
        return render_to_response('signup.html', locals())


def sregister(request):
    if request.method == 'POST' and request.POST:
        s = Servers()
        s.hostname = request.POST['hostname']
        s.ip = request.POST['ip']
        s.mac = request.POST['mac']
        s.cpu = request.POST['cpu']
        s.mem = request.POST['mem']
        s.disk = request.POST['disk']
        s.system = request.POST['system']
        s.io = request.POST['io']
        s.lastLogin = request.POST['lastLogin']
        s.lastLoginUser = request.POST['lastLoginUser']
        s.isActive = 'N'
        s.save()
        return HttpResponseRedirect(reverse('serverlist'))

    return render_to_response('sregister.html', locals())


def serverList(request):
    serverlist = Servers.objects.all()
    return render_to_response('serverlist.html', locals())

def serverGroup(request):
    sgroup = ServerGroup.objects.all()
    return render_to_response('servergroup.html', locals())

def serverdetail(request, sid):
    serverinfo = Servers.objects.get(id = int(sid))
    return render_to_response('serverdetail.html', locals())


def delServer(request):
    if request.method == 'POST' and request.POST:
        id = int(request.POST['id'])
        server = Servers.objects.get(id = id)
        server.delete()
        return JsonResponse({'status': 'success'})
    else:
        return JsonResponse({'status': 'error'})

def delGroup(request):
    if request.method == 'POST' and request.POST:
        id = int(request.POST['id'])
        group = ServerGroup.objects.get(id = id)
        group.delete()
        return JsonResponse({'status': 'success'})
    else:
        return JsonResponse({'status': 'error'})


def addGroup(request):
    if request.method == 'POST' and request.POST:
        g = ServerGroup()
        g.groupname = request.POST['groupname']
        g.description = request.POST['description']
        g.memberCount = 0
        g.save()
        return HttpResponseRedirect(reverse('servergroup'))

    return render_to_response('addgroup.html', locals())


def groupdetail(request, sid):
    groupinfo = ServerGroup.objects.get(id = int(sid))
    serverlist = Servers.objects.all()
    return render_to_response('groupdetail.html', locals())

def addServerToGroup(request, gid):
    groupinfo = ServerGroup.objects.get(id = int(gid))
    return render_to_response('addServerToGroup.html', locals())


@valid_login
def index(request):
    username = request.session['username']
    return render_to_response('index.html', locals())

def base(request):
    username = 'abc'
    return render_to_response('base.html', locals())